var config = require('../../utils/config');
var util = require('../../utils/util');
Page({
  data: {
    bookList:[],
    inputValue:'',
    grades:[],
    curGrade:'',
    subjects:[],
    curSubject:'',
    disparts:[],
    curDispart:'',
    pageNo:1,
    pageSize:10,
    dropDownMenuTitle: ['全部年级', '全部学科', '全部分册']
  },
  queryGrades:function(e) {
    var that = this;
    var options = {
      url: config.clubApi.gradeList,      
    };
    util.request(options, (res, err) => {
      var grades = [{id:"",title:"全部年级"}];
      for (var i = 0; i < res.data.data.length; i++) {
        grades.push({id:res.data.data[i], title:res.data.data[i]});
      }
      that.setData({
        grades: grades
      });
    });
  },
  querySubjects:function(e) {
    var that = this;
    var options = {
      url: config.clubApi.subjectList,      
    };
    util.request(options, (res, err) => {
      var subjects = [{id:"",title:"全部学科"}];
      for (var i = 0; i < res.data.data.length; i++) {
        subjects.push({id:res.data.data[i], title:res.data.data[i]});
      }
      that.setData({
        subjects: subjects
      });
    });
  },
  queryDisparts:function(e) {
    var that = this;
    var options = {
      url: config.clubApi.dispartList,      
    };
    util.request(options, (res, err) => {
      var disparts = [{id:"",title:"全部分册"}];
      for (var i = 0; i < res.data.data.length; i++) {
        disparts.push({id:res.data.data[i], title:res.data.data[i]});
      }
      that.setData({
        disparts: disparts
      });
    });
  },
  selectGradeItem: function(e) {
    var that = this;
    that.setData({
      curGrade: e.detail.selectedId
    });
  },
  selectSubjectItem: function(e) {
    var that = this;
    that.setData({
      curSubject: e.detail.selectedId
    });
  },
  selectDispartItem: function(e) {
    var that = this;
    that.setData({
      curDispart: e.detail.selectedId
    });
  },
  inputChange:function(e) {
    this.data.inputValue = e.detail.value;
  },
  queryBooks:function() {
    var that = this;
    var options = {
      url: config.clubApi.list,
      data: {
        title: that.data.inputValue,
        grade: that.data.curGrade,
        subject: that.data.curSubject,
        dispart: that.data.curDispart,
        userId: '0',
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize
      }
    };
    util.request(options, (res, err) => {
      var books = res.data.data.pageData;
      that.setData({
        bookList: books
      });
    });
  },
  goToDetailPage:function(e) {
    var id = e.currentTarget.id;
    var qty = e.currentTarget.dataset.qty;
    wx.navigateTo({
      url: '../detail/detail?id='+id+'&qty='+qty
    });
  },
  onLoad:function() {
    // 页面显示
    this.queryGrades();
    this.querySubjects();
    this.queryDisparts();
    this.queryBooks();
  },
  onReachBottom(){
    setTimeout(()=>{
      this.getMoreData();
    },500)
  },
  getMoreData() {
    var that = this;
    that.data.pageNo++;
    var options = {
      url: config.clubApi.list,
      data: {
        title: that.data.inputValue,
        grade: that.data.curGrade,
        subject: that.data.curSubject,
        dispart: that.data.curDispart,
        userId: '0',
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize
      }
    };
    util.request(options, (res, err) => {
      var books = res.data.data.pageData;
      that.setData({
        bookList: books
      });
    });
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
})